<?php
namespace app\seller\controller;
use app\seller\Controller;
use pidan\facade\Db;
use app\seller\model\Task as TaskModel;

class Task extends Controller
{
	//取任务
	public function get()
	{
		$region=input('request.region',0,'intval');//$region=step
		$id=input('request.id',0,'intval');
		if(!$id){
			return $this->code(1,lang('invalid request'));
		}

		$row = TaskModel::find($id);

		$res=[['id'=>$row->id,'step'=>$row->step,'maxstep'=>$row->maxstep]];

		if(empty($region) || $region==1) {
			$res[1]=['id'=>$row->id,'mall_id'=>$row->mall_id,'type_id'=>$row->type_id,'user_id'=>$row->user_id];
		}
		if($row->step>=2 && (empty($region) || $region==2)){
			$res[2]=[
				'store_id'=>$row->store_id
				,'goodsname[0]'=>htmlquotes($row->goodsname,1)
				,'goodsurl[0]'=>htmlquotes($row->goodsurl,1)
				,'goodsimage[0]'=>htmlquotes($row->goodsimage,1)
				,'goodskeyword[0]'=>htmlquotes($row->goodskeyword,1)
				,'goodsprice[0]'=>$row->goodsprice
				,'goodsnum[0]'=>$row->goodsnum
				,'ordermsg'=>htmlquotes($row->ordermsg,1)
				,'id'=>$row->id
				,'moregoods'=>$row->moregoods
			];
			if($row->moregoods){
				$rows=Db::name('task_goods')->where('task_id',$row->id)->select();
				//附加商品
				for($i=1;$i<=$row->moregoods;$i++){
					$subscript='['.$i.']';
					$res[2]['goodsname'.$subscript]=htmlquotes($rows[$i-1]['goodsname'],1);
					$res[2]['goodsurl'.$subscript]=htmlquotes($rows[$i-1]['goodsurl'],1);
					$res[2]['goodsimage'.$subscript]=htmlquotes($rows[$i-1]['goodsimage'],1);
					$res[2]['goodskeyword'.$subscript]=htmlquotes($rows[$i-1]['goodskeyword'],1);
					$res[2]['goodsprice'.$subscript]=$rows[$i-1]['goodsprice'];
					$res[2]['goodsnum'.$subscript]=$rows[$i-1]['goodsnum'];
				}
			}
		}
		return $this->api($res);
	}

	public function add1(){//第一步新增
		$data=[];
		$data['mall_id']=input('request.mall',0,'intval');
		$data['type_id']=input('request.type',0,'intval');
		$data['user_id']=token_session('user_id');
		$data['step']=1;
		$data['maxstep']=1;
		$data['status']=5;//临时

		$msg='';
		//平台不存在
		if($data['mall_id']==0 || Db::name('mall')->where('id',$data['mall_id'])->count('id') == 0 ){
			$msg='平台不存在';
		}
		//任务类型不存在
		elseif($data['type_id']==0 || Db::name('type')->where('id',$data['type_id'])->where('mall_id',$data['mall_id'])->count('id') == 0 ){
			$msg='任务类型不存在';
		}
		if(!empty($msg)){
			return $this->code(1,$msg);
		}
		//print_r($data);exit;
		Db::startTrans();
		try {
			$task           = new TaskModel;
			$task->save($data);
			$result=['code'=>0,'msg'=>'','data'=>['id'=>$task->id,'mall_id'=>$task->mall_id,'type_id'=>$task->type_id,'user_id'=>$task->user_id] ];
			Db::commit();
		} catch (\Exception $e){
			Db::rollback();
			$result=['code'=>2,'msg'=>$e->getMessage()];
		}
		return $this->result($result);
	}
	//任务提交第一步
	public function add2(){

		$datum=$data=[];
		$id=input('request.id',0,'intval');
		$data['step']=2;
		$data['store_id']=input('request.store_id',0,'intval');
		$data['ordermsg']=input('request.ordermsg','','trim,strip_tags,htmlquotes');
		//附加商品
		$datum['goodsname']=input('request.goodsname','','trim,strip_tags,htmlquotes');
		$datum['goodsurl']=input('request.goodsurl','','trim,strip_tags,htmlquotes');
		$datum['goodsimage']=input('request.goodsimage','','trim,strip_tags,htmlquotes');
		$datum['goodskeyword']=input('request.goodskeyword','','trim,strip_tags,htmlquotes');
		$datum['goodsprice']=input('request.goodsprice',0,'minus0,int100');
		$datum['goodsnum']=input('request.goodsnum',0,'minus0,intval');

		$data['goodsname']=$datum['goodsname'][0];
		$data['goodsurl']=$datum['goodsurl'][0];
		$data['goodsimage']=$datum['goodsimage'][0];
		$data['goodskeyword']=spacesToOne($datum['goodskeyword'][0]);
		$data['goodsprice']=$datum['goodsprice'][0];
		$data['goodsnum']=$datum['goodsnum'][0];


		$msg='';
		$total=count($datum['goodsname']);

		$row = TaskModel::find($id);
		$row->maxstep>2 ?:$data['maxstep']=2;

		//临时任务找不到
		if(empty($row)){
			$msg='此任务已失效';
		}
		//附加商品大于2个
		elseif($total>3){
			$msg='最多加2个附加商品';
		}
		//平台与店铺不匹配
		elseif(empty($data['store_id']) || Db::name('store')->where('id',$data['store_id'])->where('mall_id',$row['mall_id'])->count('id') == 0 ){
			$msg='平台与店铺不匹配';
		}
		elseif(!is_array($datum['goodsname'])||!is_array($datum['goodsurl'])||!is_array($datum['goodsimage'])||!is_array($datum['goodskeyword'])||!is_array($datum['goodsprice'])||!is_array($datum['goodsnum'])){
			$msg='参数不对';
		}
		elseif($total!=count($datum['goodsurl'])||$total!=count($datum['goodsimage'])||$total!=count($datum['goodskeyword'])||$total!=count($datum['goodsprice'])||$total!=count($datum['goodsnum'])) {
			$msg = '参数不对';
		}
		elseif(in_array('',$datum['goodsname'])){
			$msg='请输入商品名称';
		}
		elseif(in_array('',$datum['goodsurl'])){
			$msg='请输入商品链接';
		}
		elseif(in_array('',$datum['goodsimage'])){
			$msg='请上传商品主图';
		}
		elseif(in_array('',$datum['goodskeyword'])){
			$msg='请输入商品详情页中答案词';
		}
		elseif(in_array(0,$datum['goodsprice'])){
			$msg='请输入单品售价';
		}
		elseif(in_array(0,$datum['goodsnum'])){
			$msg='请输入每单拍';
		}

		if(!empty($msg)){
			return $this->code(1,$msg);
		}

		Db::startTrans();
		try {
			//有一个失败操作中止运行 回滚
			$status=false;
			$more_goods_num=0;//附加商品数量    缓冲处理
			if($row->save($data)){
				$status=true;
				//商品主图
				move_file($data['goodsimage']);
				//保存附加商品
				Db::name('task_goods')->where('task_id',$row->id)->delete();//先清除之前保存的
				foreach($datum['goodsname'] as $k => $v){
					if($k==0 || $k>2 || empty($v)) continue;
					move_file($datum['goodsimage'][$k]);
					$status=Db::name('task_goods')->insert(array('task_id'=>$row->id,'goodsname'=>$datum['goodsname'][$k],'goodsurl'=>$datum['goodsurl'][$k],'goodsimage'=>$datum['goodsimage'][$k],'goodskeyword'=>spacesToOne($datum['goodskeyword'][$k]),'goodsprice'=>$datum['goodsprice'][$k],'goodsnum'=>$datum['goodsnum'][$k],'createtime'=>time()));
					if(!$status) break;
					$more_goods_num++;
				}
				if($status){//没有入库失败才成功
					if($more_goods_num>0)Db::name('task')->where('id',$row->id)->save(array('moregoods'=>$more_goods_num));
				}
			}

			if($status){//有一个失败操作中止运行 回滚
				$result=['code'=>0,'msg'=>'','data'=>['moregoods'=>$more_goods_num]];
				Db::commit();
			}
			else{
				$result=['code'=>2,'msg'=>'保存失败,请重试'];
				Db::rollback();
			}
		} catch (\Exception $e) {
			Db::rollback();
			$result=['code'=>3,'msg'=>$e->getMessage()];
		}
		return $this->result($result);
	}
public function add3(){

		$data=[];
		$id=input('request.id',0,'intval');
		$data['step']=3;
		$data['sortstyle']=input('request.sortstyle',0,'intval');
		$data['listprice']=input('request.listprice',0,'int100');
		$data['commentstar']=input('request.commentstar',0,'floatval');
		$data['commentnum']=input('request.commentnum',0,'intval');
		$data['isfilter']=input('request.commentnum',0,'int01');
		$data['iscoupon']=input('request.iscoupon',0,'int01');

		$msg='';
		$row = TaskModel::find($id);
		$row->maxstep>3 ?:$data['maxstep']=3;

		//临时任务找不到
		if(empty($row)){
			$msg='此任务已失效';
		}
		//平台与定位目标商品排序不匹配
		elseif(!in_array($data['sortstyle'],array_flip(lang('sortstyle')[$row->mall_id]))){
			$msg='平台与定位目标商品排序不匹配';
		}
		elseif(empty($data['listprice'])){
			$msg='请输入手机搜索页展示价格';
		}
		elseif(int10($data['commentstar'])<0 || int10($data['commentstar'])>50) {
			$msg ='请输入商品评论星级';
		}
		elseif(empty($data['commentnum'])){
			$msg='请输入商品现有评价数';
		}

		if(!empty($msg)){
			return $this->code(1,$msg);
		}

		$row->save($data);//这里
		$result=['code'=>0,'msg'=>''];

		return $this->result($result);
	}

}